package operation;

import com.supermap.desktop.controls.ui.UICommonToolkit;
import com.supermap.desktop.controls.ui.trees.LayersTree;
import util.Info;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import java.util.ArrayList;

public class LayerTreeOperation extends TreeOperation {
	ArrayList<TreeNode> treeNodes = new ArrayList<TreeNode>();
	LayersTree tree = UICommonToolkit.getLayersManager().getLayersTree();

	public void selectPopupMenuItem(String menu) throws Exception {
		if(treeNodes!= null && treeNodes.size()>0){
			TreeOperation.selectPopupMenuItem((DefaultMutableTreeNode) treeNodes.get(treeNodes.size() - 1),menu);
		}
		else {
			Info.out("图层树节点未找到:" + menu);
			throw new Exception("工作空间树节点未找到:" + menu);
		}
	}

	/**
	 *
	 * @param layerNames 图层名称，多个图层以“|”隔开，表示正则表达式的“或”
	 * @return
	 */
	public ArrayList<TreeNode> selectLayerNode(String layerNames){
		ArrayList<SearchMap> searchMapList = new ArrayList<SearchMap>();
		SearchMap searchMap = new SearchMap(SearchType.TEXT, layerNames);
		searchMapList.add(searchMap);
		treeNodes = TreeOperation.selectTreeNodes(tree,searchMapList);
		return treeNodes;
	}

	public ArrayList<TreeNode> selectLayerNodeMenu(String layerName,String menu) throws Exception {
		this.selectLayerNode(layerName);
		this.selectPopupMenuItem(menu);
		return treeNodes;
	}

}
